
package com.example.geguo.nettyclientchatdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.geguo.nettyclientchatdemo.core.OnReceiveMsgListener;

import io.netty.channel.Channel;


public class MainActivity extends AppCompatActivity {

	private static final String TAG ="MainActivity";
	private Channel client;
	private EditText editText;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) findViewById(R.id.editText);
		textView = (TextView) findViewById(R.id.textView);
	}

	/*public void connect(View view){
		Observable.create(new Observable.OnSubscribe<Channel>() {
			@Override
			public void call(Subscriber<? super Channel> subscriber) {
				EventLoopGroup group = new NioEventLoopGroup();
				try {
					Bootstrap bootstrap  = new Bootstrap()
							.group(group)
							.channel(NioSocketChannel.class)
							.handler(new ChannelInitializer<SocketChannel>() {
								@Override
								protected void initChannel(SocketChannel ch) throws Exception {
									ChannelPipeline pipeline = ch.pipeline();

									pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
									pipeline.addLast("decoder", new StringDecoder());
									pipeline.addLast("encoder", new StringEncoder());
									pipeline.addLast("handler", new SimpleChannelInboundHandler<String>() {
										@Override
										protected void channelRead0(ChannelHandlerContext channelHandlerContext, final String s) throws Exception {
											Log.d(TAG, "channelRead0: "+s);
											runOnUiThread(new Runnable() {
												@Override
												public void run() {
													textView.setText("receive: "+s);
												}
											});
										}
									});
								}
							});
					Channel channel = bootstrap.connect("192.168.0.104", 8080).sync().channel();
					subscriber.onNext(channel);
					channel.closeFuture().sync();//哈哈 我这里不让你走
				} catch (InterruptedException e) {
					e.printStackTrace();
					subscriber.onError(e);
					Log.e(TAG, "call: 连接异常" );
				} finally {
					group.shutdownGracefully();
					subscriber.onCompleted();
				}
			}
		}).subscribeOn(Schedulers.newThread())
		  .observeOn(AndroidSchedulers.mainThread())
		  .subscribe(new Subscriber<Channel>() {
			  @Override
			  public void onCompleted() {

			  }

			  @Override
			  public void onError(Throwable e) {

			  }

			  @Override
			  public void onNext(Channel channel) {
				  client = channel;
			  }
		  });
	}*/
	public void connect(View view){
		NettyConnectionManager.getInstance(this).addOnReceiveMsgListener(new OnReceiveMsgListener() {
			@Override
			public void onReceive(final String msg) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						textView.setText("receive: "+msg);
					}
				});
			}
		});
	}
	public void send(View view){
		NettyConnectionManager.getInstance(this).sendMsg(editText.getText().toString());

	}
/*	public void send(View view){
		if (client != null) {
			client.writeAndFlush("手机" +editText.getText().toString()+ "\r\n");
		}else {
			Toast.makeText(MainActivity.this,"发送失败",Toast.LENGTH_SHORT).show();
		}
	}*/
}
